package com.aabte.multienvironmentdemo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@SpringBootApplication
public class MultiEnvironmentDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MultiEnvironmentDemoApplication.class, args);
    }

    @Autowired
    private Demo demo;

    @GetMapping(value = "/demo")
    public Demo demo() {
        return demo;
    }

    @ConfigurationProperties(prefix = "demo")
    @Data
    @Component
    class Demo implements Serializable {
        private static final long serialVersionUID = 1945393510331553671L;
        private String envName;
    }


    @Autowired
    private Env env;

    @GetMapping(value = "/env")
    public Env env() {
        return env;
    }

    interface Env extends Serializable {
    }

    @Profile(value = {"dev"})
    @Data
    @Component
    class EnvDev implements Env {
        private static final long serialVersionUID = 3802511671719042320L;
        @Value(value = "${demo.env-name}")
        private String envName;
    }

    @Profile(value = {"test"})
    @Data
    @Component
    class EnvTest implements Env {
        private static final long serialVersionUID = 3802511671719042320L;
        @Value(value = "${demo.env-name}")
        private String envName;
    }

    @Profile(value = {"uat"})
    @Data
    @Component
    class EnvUat implements Env {
        private static final long serialVersionUID = 3802511671719042320L;
        @Value(value = "${demo.env-name}")
        private String envName;
    }

    @Profile(value = {"pro"})
    @Data
    @Component
    class EnvPro implements Env {
        private static final long serialVersionUID = 3802511671719042320L;
        @Value(value = "${demo.env-name}")
        private String envName;
    }

}
